#######################################################################
# SConscript for glapi


from sys import executable as python_cmd

Import('*')

env = env.Clone()

env.MSVC2013Compat()

env.Append(CPPDEFINES = [
    'MAPI_MODE_UTIL',
])

if env['platform'] == 'windows':
    env.Append(CPPDEFINES = [
        '_GDI32_', # prevent gl* being declared __declspec(dllimport) in MS headers
        'BUILD_GL32', # declare gl* as __declspec(dllexport) in Mesa headers
        'KHRONOS_DLL_EXPORTS', # declare gl* as __declspec(dllexport) in Khronos headers
        '_GLAPI_NO_EXPORTS', # prevent _glapi_* from being declared __declspec(dllimport)
    ])

env.Append(CPPPATH = [
    '#/src',
    '#/src/mapi',
    '#/src/mesa',
    Dir('.'), # src/mapi/glapi build path
    Dir('gen'), # src/mapi/glapi/gen build path
])

glapi_sources = [
    'glapi_dispatch.c',
    'glapi_entrypoint.c',
    'glapi_getproc.c',
    'glapi_nop.c',
    'glapi.c',
]

mapi_sources = [
    'u_current.c',
    'u_execmem.c',
]
for s in mapi_sources:
    o = env.SharedObject(s[:-2], '../' + s)
    glapi_sources.append(o)

#
# Assembly sources
#
if env['platform'] not in ('cygwin', 'darwin', 'windows'):
    GLAPI = '#src/mapi/glapi/'
    sources = [GLAPI + 'gen/gl_and_es_API.xml'] + env.Glob(GLAPI + 'gen/*.xml')

    if env['machine'] == 'x86':
        env.Append(CPPDEFINES = [
            'USE_X86_ASM',
        ])
        glapi_sources += [
            'glapi_x86.S',
        ]
        env.CodeGenerate(
            target = 'glapi_x86.S',
            script = GLAPI + 'gen/gl_x86_asm.py',
            source = sources,
            command = python_cmd + ' $SCRIPT -f $SOURCE > $TARGET'
            )
    elif env['machine'] == 'x86_64':
        env.Append(CPPDEFINES = [
            'USE_X86_64_ASM',
        ])
        glapi_sources += [
            'glapi_x86-64.S'
        ]
        env.CodeGenerate(
            target = 'glapi_x86-64.S',
            script = GLAPI + 'gen/gl_x86-64_asm.py',
            source = sources,
            command = python_cmd + ' $SCRIPT -f $SOURCE > $TARGET'
            )
    elif env['machine'] == 'sparc':
        env.Append(CPPDEFINES = [
            'USE_SPARC_ASM',
        ])
        glapi_sources += [
            'glapi_sparc.S'
        ]
        env.CodeGenerate(
            target = 'glapi_sparc.S',
            script = GLAPI + 'gen/gl_SPARC_asm.py',
            source = sources,
            command = python_cmd + ' $SCRIPT -f $SOURCE > $TARGET'
            )
    else:
        pass

glapi = env.ConvenienceLibrary(
    target = 'glapi',
    source = glapi_sources,
)
Export('glapi')
